<?php

namespace app\controller\admin\ah;

use app\model\api\ah\AhCareer;
use app\model\api\ah\AhEquipment;
use app\model\api\ah\AhEquipmentPosition;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class MainEquipment
 * @package app\controller\admin\ah
 */
class MainEquipment extends Backend
{
    protected $model;

    public function initialize()
    {
        $this->model = new AhEquipment();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function index()
    {
        $position_model = new AhEquipmentPosition();
        $career_model = new AhCareer();
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $searchParam = $this->request->param('search_param');
        $role_ids = [];
        if(isset($searchParam['role_id'])){
            if(!empty($searchParam['role_id']['value'])){
                $role_ids = explode(",",$searchParam['role_id']['value']);
                $role_ids = array_map(function ($item){
                    return '["'.$item.'"]';
                },$role_ids);
            }
        }
        $main_ids = $position_model->where('type','main')->column('id');
        $careers = $career_model->select()->toArray();
        $careers = array_combine(array_column($careers, 'role_id'), array_column($careers, 'name'));
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model;
            if(count($role_ids)>0){
                $data = $data->whereIn('role_id',$role_ids);
            }
             $data = $data->where($where)->whereIn('kind',$main_ids)->order($order)->cache(true)->select()->toArray();
            $data = array_map(function ($item)use($careers){
                $item['role_name'] = '';
                $item['role_id'] = json_decode($item['role_id'],true);
                foreach ($item['role_id'] as $i){
                    $item['role_name'] = $careers[$i]." ";
                }
                return $item;
            },$data);
        } else {
            $limit = $this->request->param('limit', 10);
            $data = $this->model;
            if(count($role_ids)>0){
                $data = $data->whereIn('role_id',$role_ids);
            }
            $data = $data->where($where)->whereIn('kind',$main_ids)->order($order)->paginate($limit)->toArray();
            $data['data'] = array_map(function ($item)use($careers){
                $item['role_id'] = json_decode($item['role_id'],true);
                $item['role_name'] = '';
                foreach ($item['role_id'] as $i){
                    $item['role_name'] = $careers[$i]." ";
                }
                return $item;
            },$data['data']);
        }
        return $this->success('数据获取成功', $data);
    }


    public function add()
    {
        $post     = CommonFun::filterPostData($this->request->post());
        $insData = [
            'name' => $post['name'],
            'role_id' => json_encode([$post['role_id']],256),
            'icon' => $post['icon'],
            'rarity' => $post['rarity'],
            'kind' => $post['kind'],
            'kind_desc' => (new AhEquipmentPosition())->where('id',$post['kind'])->value("name"),
            'level_limit' => $post['level_limit'],
            'score' => $post['score'],
            'description' => $post['description'],
            'attr_name' => $post['attr_name'],
            'attr_value' => $post['attr_value'],
            'legendary_attr' => $post['legendary_attr'],
        ];
        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function edit()
    {
        $post     = CommonFun::filterPostData($this->request->post());
        $detail = $this->model->find($post['id']);
        empty($detail) && $this->error('数据不存在');
        $detail->name = $post['name'];
        $detail->role_id = json_encode([$post['role_id']],256);
        $detail->icon = $post['icon'];
        $detail->rarity = $post['rarity'];
        $detail->kind = $post['kind'];
        $detail->kind_desc =  (new AhEquipmentPosition())->where('id',$post['kind'])->value("name");
        $detail->level_limit = $post['level_limit'];
        $detail->score = $post['score'];
        $detail->description = $post['description'];
        $detail->attr_name = $post['attr_name'];
        $detail->attr_value = $post['attr_value'];
        $detail->legendary_attr = $post['legendary_attr'];
        try {
            $save = $detail->save();
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        $info->role_id = json_decode($info->role_id,true)[0];
        return $this->success('获取成功', $info);
    }
}